/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 北京银杉金服科技有限公司
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2019/1/12 20:34
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.server.action.request;

import com.hyacinth.server.constant.Constant;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.cookie.ServerCookieDecoder;

import java.util.HashMap;
import java.util.Map;

/**
 * http请求对象
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
public class AuroraHttpRequest implements AuroraRequest{

    private String method;

    private String url;

    private String clientAddress;

    private Map<String,Cookie> cookie = new HashMap<>(8);

    private Map<String,String> headers = new HashMap<>(8);

    private AuroraHttpRequest(){}

    /**
     * 初始化请求参数
     * @param httpRequest netty默认http请求
     * @return 封装请求
     */
    public static AuroraHttpRequest init(DefaultHttpRequest httpRequest) {
        // 创建请求对象
        AuroraHttpRequest request = new AuroraHttpRequest();
        request.method = httpRequest.method().name();
        request.url = httpRequest.uri();

        // 构建headers
        buildHeaders(httpRequest, request);

        // 初始化cookie
        initCookies(request);

        return request;
    }

    /**
     * 初始化cookie
     * @param request 封装请求
     */
    private static void initCookies(AuroraHttpRequest request) {
        for (Map.Entry<String, String> entry: request.headers.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (!Constant.ContentType.COOKIE.equals(key)) {
                continue;
            }

            // 设置cookie
            for (io.netty.handler.codec.http.cookie.Cookie cookie : ServerCookieDecoder.LAX.decode(value)) {
                Cookie auroraCookie = new Cookie();
                auroraCookie.setPath(cookie.path());
                auroraCookie.setName(cookie.name());
                auroraCookie.setValue(cookie.value());
                auroraCookie.setMaxAge(cookie.maxAge());
                auroraCookie.setDomain(cookie.domain());
                request.cookie.put(auroraCookie.getName(), auroraCookie);
            }
        }
    }

    /**
     * 构建请求对象
     * @param httpRequest http请求
     * @param request 封装请求
     */
    private static void buildHeaders(DefaultHttpRequest httpRequest, AuroraHttpRequest request) {
        for (Map.Entry<String, String> entry : httpRequest.headers().entries()) {
            request.headers.put(entry.getKey(), entry.getValue());
        }
    }


    @Override
    public String getMethod() {
        return this.method;
    }

    @Override
    public String getUrl() {
        return this.url;
    }

    @Override
    public Cookie getCookie(String key) {
        return this.cookie.get(key);
    }

    @Override
    public String toString() {
        return "AuroraHttpRequest{" +
                "method='" + method + '\'' +
                ", url='" + url + '\'' +
                ", clientAddress='" + clientAddress + '\'' +
                ", cookie=" + cookie +
                ", headers=" + headers +
                '}';
    }
}
